<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-26
 * Time: 下午2:59
 * To change this template use File | Settings | File Templates.
 */
class ffdy_cc extends MY_Model
{
    private $data = array();

    /**
     * 分析数据
     *
     * @param array $arr
     * @return bool
     */
    public function analysis(array $arr)
    {
        $content = $this->getFileContent($arr['file_name']);
        $download = array();

        if (empty ($content)) return false;

        //来源网站ID
        $this->data['source_id'] = $arr['source_id'];

        preg_match("/当前位置.*<a href=\"\/type\/.*\/\">(.*)<\/a>/sU", $content, $class);
        $this->data['class'] = empty ($class[1]) ? '' : $this->getClass($class[1]);

//echo '<pre>';print_r($this->data);echo $content;exit;
        //中英文名称
        preg_match("/<div id=\"film\">.*<h1>(.*)<em>(.*)<\/em>/sU", $content, $name);
        $this->data['china_name'] = empty ($name[1]) ? '' : $name[1];
        $this->data['english_name'] = empty ($name[2]) ? '' : $name[2];

        //获取代码块
        preg_match("/<div class=\"detail_intro\">.*<\/table>/sU", $content, $codeBlock);

        //导演
        preg_match("/导演.*<\/td>(.*)<\/td>/sU", $codeBlock[0], $direct);
        if (!isset ($direct[1])) {
            $this->data['director'] = '';
        } else {
            preg_match_all("/<a.*>(.*)<\/a>/sU",$direct[1], $director);
            $this->data['director'] = implode( '|', array_filter($director[1]) );
        }

        //主演
        preg_match("/主演.*<\/td>.*<\/td>/sU", $codeBlock[0], $starr);//主演
        if (!isset ($starr[0])) {
            $this->data['starring'] = '';
        } else {
            preg_match_all("/<a.*>(.*)<\/a>/sU", $starr[0], $starring);
            $this->data['starring'] = implode('|', array_filter($starring[1]));
        }

        //地区
        preg_match("/国家\/地区.*<a.*>(.*)<\/a>/sU", $codeBlock[0], $country);
        $this->data['area'] = empty ($country[1]) ? '' : $country[1];

        //类型
        preg_match("/类型.*<\/td>.*<\/td>/sU", $codeBlock[0], $type);
        if (!isset ($type[0])) {
            $this->data['type'] = '';
        } else {
            preg_match_all("/<a.*>(.*)<\/a>/sU", $type[0], $types);
            $this->data['type'] = implode('|', array_filter($types[1]));
        }

        //上映日期
        preg_match("/上映日期.*<td.*>(.*)<\/td>/sU", $codeBlock[0], $releases);
        $this->data['public_time'] = empty ($releases[1]) ? '' : $releases[1];

        //版本
        preg_match("/版本.*<\/td>.*<td.*>(.*)<\/td>/sU", $codeBlock[0], $version);
        /*if (isset ($version[1]) && mb_strpos($version[1], '高清', 0, 'utf-8') !== false) { $versions = 2; }
        else if (isset ($version[1]) && mb_strpos($version[1], 'DVD', 0, 'utf-8') !== false){ $versions = 1; }
        else if (isset ($version[1]) && mb_strpos($version[1], '抢鲜', 0, 'utf-8') !== false) { $versions = 3; }
        else if (isset ($version[1]) && mb_strpos($version[1], 'VCD', 0, 'utf-8') !== false) { $versions = 4; }
        else { $versions = 1; }//*/
        $this->data['version'] = empty ($version[1]) ? 'DVD' : $version[1];

        //对白语言
        preg_match("/对白语言.*<\/td>.*<td.*>(.*)<\/td>/sU", $content, $dialog);
        $this->data['language'] = empty ($dialog[1]) ? '' : $dialog[1];

        //简介
        preg_match("/<p class=\"inner_content\">(.*)<\/p>/sU", $content, $summary);
        if (empty ($summary[1])) {
	        $summary = array();
	        preg_match("/<div class=\"describe\" twffan=\"done\">(.*)<\/div>/sU", $content, $summary);
	    }
        $this->data['drama'] = empty ($summary[1]) ? '' : $summary[1];

        //集数
        preg_match("/集数.*<\/td>.*<td.*>(.*)<\/td>/sU", $codeBlock[0], $updatePart);
        $this->data['episode'] = empty ($updatePart) ? 1 : $updatePart[1];

        //配音
        preg_match("/配音.*<\/td>(.*)<\/td>/sU", $codeBlock[0], $dub);
        if (!isset ($dub[1])) {
            $this->data['dub'] = '';
        } else {
            preg_match_all("/<a.*>(.*)<\/a>/sU", $dub[1], $dubs);
            $this->data['dub'] = implode('|', array_filter($dubs[1]));
        }

        //海报地址
        preg_match("/<span class=\"detail_pic1\">.*<img src=\"(.*)\".*>.*<\/span>/sU", $content, $pic);
        $this->data['img_addr'] = empty ($pic[1]) ? '' : $pic[1];

        //下载地址
        preg_match("/<div class=\"resourcesmain\">(.*)<\/table>/sU", $content, $downList);
        preg_match_all("/<tr.*><td.*>.*<input.*value.*=\"(.*)\".*>.*<a.*>(.*)<\/a>.*<td.*>(.*)<\/td>/sU", $downList[1], $downLists);

        foreach ($downLists[1] as $k=>$v) {
            $download[] = array('name'=>$downLists[2][$k], 'downaddr'=>$v, 'size' => $downLists[3][$k]);
        }
        $this->data['download'] = $download;

        //echo '<pre>';print_r($this->data['download']);exit;
        //echo '<pre>';print_r($this->data);
        //echo $content;
    }

    /**
     * 保存数据
     *
     * @return mixed
     */
    public function save()
    {
        //echo '<pre>';print_r($this->data);exit;
        $data = array(
            'china_name' => $this->data['china_name'],
            'english_name' => $this->data['english_name'],
            'class' => $this->data['class'],
            'director' => $this->data['director'],
            'starring' => $this->data['starring'],
            'area' => $this->data['area'],
            'type' => $this->data['type'],
            'update_status' => 1,
            'public_time' => $this->data['public_time'],
            'version' => $this->data['version'],
            'language' => $this->data['language'],
            'poster_addr' => '',
            'drama' => addslashes($this->data['drama']),
            'episode' => $this->data['episode'],
            'dub' => $this->data['dub'],
            'status' => 1,
            'uniqid' => md5('ffdy_cc'.$this->data['source_id']),
            'img_addr' => $this->data['img_addr'],
            'source' => 'ffdy_cc',
            'source_id' => $this->data['source_id'],
            'score' => '',
            'subtitle' => '',
            'release' => '',
            'create_time' => date('Y-m-d H:i:s'),
        );

        $this->db->insert('movie', $data, true);
        $mId = $this->db->insert_id();
        if ($mId) {
            $this->categoryCorrespond($mId, $this->data['class'], $this->data['type']);
        }
        $this->saveDownLoad($mId);

        return $mId;
    }

    /**
     * 保存下载链接地址
     *
     * @param $mId
     */
    public function saveDownLoad($mId)
    {
        if (empty ($this->data['download']) || !is_array($this->data['download'])) {
            $this->data['download'] = array();
        }
        if (empty ($mId)) {
            $this->load->model('movie');

            $data = $this->movie->getMovieByChinaName($this->data['china_name']);
            if (!empty ($data)) {
                $mId = $data['id'];
            }
        }

        foreach ($this->data['download'] as $v) {
            if (empty ($v) || !is_array($v)) continue;

            parse_str($v['downaddr'], $tmpArr);
            //echo '<pre>';print_r($tmpArr);//exit;

			$str = '';
			isset ($tmpArr['cid']) && $str .= 'cid='.$tmpArr['cid'];
			isset ($tmpArr['xzurl']) && $str .= '&xzurl='.$tmpArr['xzurl'];
			$str .= '&mc='.$v['name'];

			//echo $str."\n";exit;
            $downloadLink = getDownLoadUrl($str);
            $data = array(
                'video_id' => $mId,
                'name' => $v['name'],
                'download_addr' => $downloadLink,//
                'size' => $v['size'],
                'uniqueid' => md5($mId.$v['name'].$downloadLink.$v['size']),
                'source_download_link' => 'http://thunder.ffdy.cc/'.$tmpArr['cid'].'/'.$v['name'],
                'create_time' => date('Y-m-d H:i:s'),
            );
            //echo '<pre>';print_r($data);exit;
            $this->db->insert('download_list', $data, true);
            $downloadId = $this->db->insert_id();
            if ($downloadId) {
                $this->updateResource($mId);
            }
        }
        unset ($this->data['download']);
    }
}
